var upserver = '/api/uploadpm';
var saveType = 'PM';
var downloadUrl = '/api/downUserFiles/125983256988';
var tabType = 'PM type';
var tabTypeTdName = 'PMType';
var idType = 'pm_id';
var Isotopic = 'Isotopic fingerprints';
var upload = {
	downloadTemplate:function(){
		//ui.confirm("Confirm download?",function(){
			window.open(downloadUrl);
		//});
		//修改layer弹窗，中文“信息”修改为"message"
		//$(".layui-layer-title").text("Message");
	},
	loadList:function(){
		var config = {
				url:'/uploadList',
				data:{},
				obj:"#content",
				callBackFunc:function(){
					$("#content").attr("class","pupload-tb n-box");
					$("#tabType").text(tabType);
					$("#isotopic").text(Isotopic);
					if(saveType == 'SOURCE'){
						//隐藏两列。添加一列
						$("#ct").hide();
						$("#sd").hide();
						$("#rf").after("<th>Note</th>");
					}
				},
				callBackErrorFunc:function(){}
		};
		HD.loadAsy(config);
	},
	addOneRow:function(){
		var row = "";
		if(saveType == 'PM'){
			row = "<tr>"+
				"<td style='display:none'><input type='text' name='EleType'/></td>"+
				"<td><input type='text' name='No'/></td>"+
				"<td><input type='text' name='IsotopeType'/></td>"+
				"<td><input type='text' name='PMType'/></td>"+
				"<td><input type='text' name='Content'/></td>"+
				"<td><input type='text' name='Ic'/></td>"+
				"<td><input type='text' name='icsd'/></td>"+
				"<td><input type='text' name='n'/></td>"+
				"<td><input type='text' name='mc'/></td>"+
				"<td><input type='text' name='mcsd'/></td>"+
				"<td><input type='text' name='SamplingDate'/></td>"+
				"<td><input type='text' name='Year'/></td>"+
				"<td><input type='text' name='Season'/></td>"+
				"<td><input type='text' name='SamplingSite'/></td>"+
				"<td><input type='text' name='Nation'/></td>"+
				"<td><input type='text' name='Region'/></td>"+
				"<td><input type='text' name='Latitude'/></td>"+
				"<td><input type='text' name='Longitude'/></td>"+
				"<td><input type='text' name='Laboratory'/></td>"+
				"<td><input type='text' name='Reference'/></td>"+
				"</tr>";
		}else{
			row = "<tr>"+
				"<td style='display:none'><input type='text' name='EleType'/></td>"+
				"<td><input type='text' name='No'/></td>"+
				"<td><input type='text' name='IsotopeType'/></td>"+
				"<td><input type='text' name='SourceType'/></td>"+
				"<td><input type='text' name='Content'/></td>"+
				"<td><input type='text' name='Ic'/></td>"+
				"<td><input type='text' name='icsd'/></td>"+
				"<td><input type='text' name='n'/></td>"+
				"<td><input type='text' name='SamplingDate'/></td>"+
				"<td><input type='text' name='Year'/></td>"+
				"<td><input type='text' name='Season'/></td>"+
				"<td><input type='text' name='SamplingSite'/></td>"+
				"<td><input type='text' name='Nation'/></td>"+
				"<td><input type='text' name='Region'/></td>"+
				"<td><input type='text' name='Latitude'/></td>"+
				"<td><input type='text' name='Longitude'/></td>"+
				"<td><input type='text' name='Laboratory'/></td>"+
				"<td><input type='text' name='Reference'/></td>"+
				"<td><input type='text' name='Note'/></td>"+
				"</tr>";
		}

		$("#upTbody").append(row);
	},
	//提交数据进行保存
	saveData:function(){
		ui.popProcess();
		var trs = $("#upTbody tr");
		if(trs.length == 0){
			layer.msg("No data,cannot be submitted ！");
			return;
		}
		var data = new Array();
		for(var x = 0 ; x < trs.length; x++){
			var tds = $(trs[x]).find("td");
			var rowData = new Object();
			for(var y = 0 ; y < tds.length ; y++){
				var input = $(tds[y]).find("input");
				var k = input.attr("name");
				var v = input.val();
				rowData[k] = v;
			}
			data.push(rowData);
		}
		d = encodeURIComponent(JSON.stringify(data));
		var config = {
				url:'/api/sd/'+saveType,
				data:{
					"data": d,
				},
				callBackFunc:function(data){
					ui.shutProcess();
					if(data == true){
						layer.msg("Save successfully !");
					}else{
						layer.msg("Save failure !");
					}
				},
				callBackErrorFunc:function(){}
		};
		HD.doPost(config);//提交
	},
	//组装数据
	makeData:function(data){
		if(data == null || data.length == 0){
			layer.msg("No data !");
			return;
		}
		var row = "";
		for(var x = 0 ; x < data.length; x++){
			var rowData = data[x];
			var rowType ='';
			var id = '';
			if(tabTypeTdName == 'PMType'){
				rowType = rowData.pmtype;
				id = rowData.pm_id;
			}else{
				rowType = rowData.sourceType;
				id = rowData.source_id;
			}
			if(saveType == 'PM'){
				row += "<tr>"+
					"<td style='display:none'><input type='hidden' name='"+idType+"' value='"+id+"'/></td>"+
					"<td style='display:none'><input type='text' name='EleType' value='"+rowData.eleType+"'/></td>"+
					"<td><input type='text' name='No' value='"+rowData.no+"'/></td>"+
					"<td><input type='text' name='IsotopeType' value='"+rowData.isotopeType+"'/></td>"+
					"<td><input type='text' name='"+tabTypeTdName+"' value='"+rowType+"'/></td>"+
					"<td><input type='text' name='Content' value='"+rowData.content+"'/></td>"+
					"<td><input type='text' name='Ic' value='"+rowData.ic+"'/></td>"+
					"<td><input type='text' name='icsd' value='"+rowData.icsd+"'/></td>"+
					"<td><input type='text' name='n' value='"+rowData.n+"'/></td>"+
					"<td><input type='text' name='mc' value='"+rowData.mc+"'/></td>"+
					"<td><input type='text' name='mcsd' value='"+rowData.mcsd+"'/></td>"+
					"<td><input type='text' name='SamplingDate' value='"+rowData.samplingDate+"'/></td>"+
					"<td><input type='text' name='Year' value='"+rowData.year+"'/></td>"+
					"<td><input type='text' name='Season' value='"+rowData.season+"'/></td>"+
					"<td><input type='text' name='SamplingSite' value='"+rowData.samplingSite+"'/></td>"+
					"<td><input type='text' name='Nation' value='"+rowData.nation+"'/></td>"+
					"<td><input type='text' name='Region' value='"+rowData.region+"'/></td>"+
					"<td><input type='text' name='Latitude' value='"+rowData.latitude+"'/></td>"+
					"<td><input type='text' name='Longitude' value='"+rowData.longitude+"'/></td>"+
					"<td><input type='text' name='Laboratory' value='"+rowData.laboratory+"'/></td>"+
					"<td><input type='text' name='Reference' value='"+rowData.reference+"'/></td>"+
					"</tr>";
			}else{
				row += "<tr>"+
					"<td style='display:none'><input type='hidden' name='"+idType+"' value='"+id+"'/></td>"+
					"<td style='display:none'><input type='text' name='EleType' value='"+rowData.eleType+"'/></td>"+
					"<td><input type='text' name='No' value='"+rowData.no+"'/></td>"+
					"<td><input type='text' name='IsotopeType' value='"+rowData.isotopeType+"'/></td>"+
					"<td><input type='text' name='"+tabTypeTdName+"' value='"+rowType+"'/></td>"+
					"<td><input type='text' name='Content' value='"+rowData.content+"'/></td>"+
					"<td><input type='text' name='Ic' value='"+rowData.ic+"'/></td>"+
					"<td><input type='text' name='icsd' value='"+rowData.icsd+"'/></td>"+
					"<td><input type='text' name='n' value='"+rowData.n+"'/></td>"+
					//"<td><input type='text' name='mc' value='"+rowData.mc+"'/></td>"+
					//"<td><input type='text' name='mcsd' value='"+rowData.mcsd+"'/></td>"+
					"<td><input type='text' name='SamplingDate' value='"+rowData.samplingDate+"'/></td>"+
					"<td><input type='text' name='Year' value='"+rowData.year+"'/></td>"+
					"<td><input type='text' name='Season' value='"+rowData.season+"'/></td>"+
					"<td><input type='text' name='SamplingSite' value='"+rowData.samplingSite+"'/></td>"+
					"<td><input type='text' name='Nation' value='"+rowData.nation+"'/></td>"+
					"<td><input type='text' name='Region' value='"+rowData.region+"'/></td>"+
					"<td><input type='text' name='Latitude' value='"+rowData.latitude+"'/></td>"+
					"<td><input type='text' name='Longitude' value='"+rowData.longitude+"'/></td>"+
					"<td><input type='text' name='Laboratory' value='"+rowData.laboratory+"'/></td>"+
					"<td><input type='text' name='Reference' value='"+rowData.reference+"'/></td>"+
					"<td><input type='text' name='Note' value='"+rowData.Note+"'/></td>"+
					"</tr>";
			}

		}
		$("#upTbody").append(row);
	}
}

$(function(){
	//数据类型切换
	$("#pmPicker").click(function(){
		upserver = '/api/uploadpm';
		saveType = 'PM';
		downloadUrl ='/api/downUserFiles/125983256988';
		tabType = 'PM type';
		Isotopic = 'Isotopic Composition';
		tabTypeTdName = 'PMType';
		idType = 'pm_id';
		//跳转至数据页
		upload.loadList();
	})
	$("#sourcePicker").click(function(){
		upserver = '/api/uploadsource';
		saveType = 'SOURCE';
		downloadUrl ='/api/downUserFiles/125983256987';
		tabType = 'Source Type';
		Isotopic = 'Isotopic Composition';
		tabTypeTdName = 'SourceType';
		idType = 'source_id';
		//跳转至数据页
		upload.loadList();
	})
})

